package com.douziit.meilianoa.base.utils;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;


/**
 * @author lh <br>
 *         date: 2019/1/11  <br>
 *         Description:返回字段反射类 <br>
 */
public class FieldFilterUtil<T> {

  /**
   * 字段过滤
   *
   * @author lh
   * @param list
   * @param filterFields
   * @param clazz
   * @return
   * @throws InstantiationException
   * @throws IllegalAccessException
   * @throws NoSuchMethodException
   * @throws SecurityException
   * @throws IllegalArgumentException
   * @throws InvocationTargetException
   */
  public List<T> getFieldFilterList(List<T> list, String filterFields, Class<T> clazz)
      throws InstantiationException, IllegalAccessException, NoSuchMethodException,
          SecurityException, IllegalArgumentException, InvocationTargetException {
    List<T> tempList = new ArrayList<>();
    if (list.size() <= 0) {
      return tempList;
    }
    if (filterFields == null || "".equals(filterFields.trim())) {
      return tempList;
    }
    // 获取字段数组
    Field[] fields = clazz.getDeclaredFields();
    for (int i = 0; i < list.size(); i++) {
      // 创建对象
      T ob = clazz.newInstance();
      for (Field field : fields) {
        // 取消每个属性的安全检查 ,否則无法获取private字段值
        field.setAccessible(true);
        // 存在即保留（确定需要筛选出来的字段）
        if (filterFields.indexOf(field.getName()) < 0){
            continue;
        }

        /*//字段名称
        field.getName();
        //字段值
        field.get(list.get(i));*/

        // 获取字段的属性
        String type = field.getGenericType().toString();
        Class typeTemp = getFieldType(type);

        // 获取属性名称
        String name = field.getName();
        String fieldName = name.substring(0, 1).toUpperCase() + name.substring(1, name.length());
        // 读取字段值,暂时不用
        // Method methodGet = ob.getClass().getMethod("get"+fieldName);
        // String value = (String) methodGet.invoke(ob);
        // 设置字段值
        Method methodSet = ob.getClass().getMethod("set" + fieldName, typeTemp);
        methodSet.invoke(ob, field.get(list.get(i)));
      }
      tempList.add(ob);
    }
    return tempList;
  }
    /**
     * 返回字段类型
     * @author zhaibin
     * @param type
     * @return
     */
    public static Class getFieldType(String type){
        Class typeTemp = null;
        if("class java.lang.String".equals(type)){
            typeTemp = String.class;
        }else if("class java.lang.Integer".equals(type)){
            typeTemp = Integer.class;
        }else if("class java.lang.Boolean".equals(type)){
            typeTemp = Boolean.class;
        }else if("class java.util.Date".equals(type)){
            typeTemp = java.util.Date.class;
        }else if("class java.lang.Double".equals(type)){
            typeTemp = Double.class;
        }else if("class java.lang.Long".equals(type)){
            typeTemp = Long.class;
        }else{
            typeTemp = Object.class;
        }
        return typeTemp;
    }

  /**
   * 测试类
   *
   * @param args
   * @throws NoSuchMethodException
   * @throws SecurityException
   * @throws IllegalArgumentException
   * @throws InvocationTargetException
   */
  public static void main(String[] args)
      throws NoSuchMethodException, SecurityException, IllegalArgumentException,
          InvocationTargetException {

   /* List<Tree> userList = new ArrayList<>();
    Tree good01 = new Tree();
    Tree good02 = new Tree();
    Tree good03 = new Tree();
    good01.setTreeId("zhang001");
    good01.setObject("objectzhang001");
    good01.setName("河南");

    good02.setTreeId("zhang002");
    good02.setObject("objectzhang002");
    good02.setName("河南02");

    good03.setTreeId("zhang003");
    good03.setObject("objectzhang003");
    good03.setName("河南03");
    userList.add(good01);
    userList.add(good02);
    userList.add(good03);

    FieldFilterUtil<Tree> fieldFilterUtil = new FieldFilterUtil<>();
    String filterFields = "treeId,name";
    // String filterFields = "name";
    try {
      List<Tree> ulist = fieldFilterUtil.getFieldFilterList(userList, filterFields, Tree.class);
      for (Tree ezsGoods : ulist) {
        System.out.println("过滤的结果：" + ezsGoods);
      }

    } catch (InstantiationException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IllegalAccessException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }*/
  }
}
